package com.synjones.zeuscloudapp.service.config;

import com.synjones.core.util.StringUtil;
import com.synjones.zeus.model.ExReqBean;
import com.synjones.zeus.model.ExRespBean;
import com.synjones.zeus.model.config.ConfigReqBean;
import com.synjones.zeus.model.config.ConfigRespBean;
import com.synjones.zeuscloudapp.bean.CodeDescPair;
import com.synjones.zeuscloudapp.eureka.ZeusConfigService;
import com.synjones.zeuscloudapp.service.AbstractDataPackServiceImpl;
import com.synjones.zeuscloudapp.util.BeanReqUtil;
import com.synjones.zeuscloudapp.util.ReqBean;
import com.synjones.zeuscloudapp.util.StatusEnum;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author yys
 * Description:
 * @date created in 11:25 2018-03-15
 * Modified by:
 */
@Service
public class ConfigService extends AbstractDataPackServiceImpl<ConfigReqBean,ConfigReqBean,ConfigRespBean,ConfigRespBean>{
    private static final Logger logger = LoggerFactory.getLogger(ConfigService.class);
    @Resource
    private ZeusConfigService zeusConfigService;
    /**
     *  获取request中内容后格式化
     *  @param reqBean HttpServletRequest
     *  @author yys
     *  Description: 获取request中内容后格式化
     *  @date 14:46 2018-03-15
     *  @return  ExReqBean<E>
     */
    @Override
    public ExReqBean<ConfigReqBean> buildReqBean(ReqBean<ConfigReqBean> reqBean) {
        ExReqBean<ConfigReqBean>  copyFrom = BeanReqUtil.copyFrom(reqBean);
        copyFrom.setData(reqBean.getData());
        return copyFrom;
    }
    /**
     *  验证提交data数据是否符合规则
     *  @param reqBean
     *  @author yys
     *  Description:验证提交data数据是否符合规则
     *  @date 17:53 2018-04-10
     *  @return null/code
     *  throws
     */
    @Override
    public CodeDescPair verifyData(ReqBean<ConfigReqBean> reqBean){
        if(reqBean != null && reqBean.getData() !=null && !StringUtil.isNullOrEmpty(reqBean.getOrganizationCode())
                && !StringUtil.isNullOrEmpty(reqBean.getImei())){
            return null;
        }else{
            CodeDescPair code = null;
            if(reqBean == null){
                logger.info("登陆reqBean参数为空");
                code = new CodeDescPair(StatusEnum.PARAMS_ERROR.getStatusCode(), StatusEnum.PARAMS_ERROR.getMessage());
            }
            return code;
        }
    }
    /**
     *  去eureka服务找到对应服务获取数据后格式化
     *  @param reqBean HttpServletRequest
     *  @author yys
     *  Description: 去eureka服务找到对应服务获取数据后格式化
     *  @date 14:49 2018-03-15
     *  @return  ExReqBean<E>
     */
    @Override
    public ExRespBean<ConfigRespBean> eureka(ExReqBean<ConfigReqBean> reqBean) {
        return zeusConfigService.getConfig(reqBean);
    }
    /**
     *  HttpServletResponse resBean中内容后格式化
     *  @param resBean HttpServletResponse
     *  @author yys
     *  Description: HttpServletResponse resBean中内容后格式化
     *  @date 14:54 2018-03-15
     *  @return  ExReqBean<E>
     */
    @Override
    public ConfigRespBean buildResBean(ExRespBean<ConfigRespBean> resBean) {
        return resBean.getData();
    }

    @Override
    public ConfigReqBean buildData(String jsonData) {
        JSONObject jsonObject = JSONObject.fromObject(jsonData);
        ConfigReqBean configReqBean = (ConfigReqBean) JSONObject.toBean(jsonObject, ConfigReqBean.class);
        return configReqBean;
    }
}
